package com.shusl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.HashMap;
import java.util.Map;

public class MybatisPlusGenerator {

    private static String projectRootPath = System.getProperty("user.dir") + "/src/main/java";
    private static String moduleRootParent = "com.shusl"; // 父包路径
    private static String moduleRootPath = "/" + moduleRootParent.replace(".", "/") + "/";

    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();

        // 1. 全局配置
        GlobalConfig globalConfig = new GlobalConfig()
                .setOutputDir(projectRootPath)
                .setAuthor("ikun")
                .setOpen(false)
                .setFileOverride(true)
                .setDateType(DateType.ONLY_DATE)
                .setSwagger2(true);
        autoGenerator.setGlobalConfig(globalConfig);

        // 2. 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setUrl("jdbc:mysql://localhost:3306/k?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8")
                .setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUsername("root")
                .setPassword("123456");
        autoGenerator.setDataSource(dataSourceConfig);

        // 3. 包配置
        PackageConfig packageConfig = new PackageConfig()
                .setParent(moduleRootParent)
                .setController("controller")
                .setService("service")
                .setServiceImpl("service.impl")
                .setMapper("mapper")
                .setEntity("entity")
                .setXml("mapper.xml");
        autoGenerator.setPackageInfo(packageConfig);

        // 4. 策略配置
        StrategyConfig strategyConfig = new StrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setLogicDeleteFieldName("deleted")
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .setControllerMappingHyphenStyle(true)
                .setEntityTableFieldAnnotationEnable(true);
        autoGenerator.setStrategy(strategyConfig);

        // 5. 模板配置（可选）
        TemplateConfig templateConfig = new TemplateConfig()
                .setController("/templates/controller.java.vm") // 自定义 Controller 模板路径
                .setMapper("/templates/mapper.java.vm")         // 自定义 Mapper 模板路径
                .setService("/templates/service.java.vm")       // 自定义 Service 模板路径
                .setServiceImpl("/templates/serviceImpl.java.vm") // 自定义 ServiceImpl 模板路径
                .setEntity("/templates/entity.java.vm");        // 自定义 Entity 模板路径
        autoGenerator.setTemplate(templateConfig);

        // 6. 注入配置（可选）
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("api", ""); // 动态注入 api 路径
                this.setMap(map);
            }
        };
        autoGenerator.setCfg(injectionConfig);
        autoGenerator.execute();
    }
}